package com.laoxiang.modules.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.laoxiang.execption.LxAdminException;
import com.laoxiang.modules.system.entity.SysUserPost;
import com.laoxiang.modules.system.mapper.SysUserPostMapper;
import com.laoxiang.modules.system.service.SysUserPostService;
import com.laoxiang.utils.AppHttpCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 用户岗位关系表(SysUserPost)表服务实现类
 *
 * @author db
 * @since 2023-09-11 17:10:31
 */
@Service
@Slf4j
public class SysUserPostServiceImpl extends ServiceImpl<SysUserPostMapper, SysUserPost> implements SysUserPostService {

    @Autowired
    private SysUserPostMapper sysUserPostMapper;

    @Override
    public void deleteUserPostByUserId(Long userId) {
        LambdaQueryWrapper<SysUserPost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SysUserPost::getUserId,userId);
        int count = sysUserPostMapper.delete(queryWrapper);
        if(count == 0){
            log.error("{}删除用户角色信息失败",userId);
            throw new LxAdminException(AppHttpCodeEnum.USER_POST_DELETE_ERROR);
        }
    }

    @Override
    public List<Long> getPostIdsByUserId(Long userId) {
        LambdaQueryWrapper<SysUserPost> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Objects.nonNull(userId),SysUserPost::getUserId,userId);
        List<SysUserPost> sysUserPosts = sysUserPostMapper.selectList(queryWrapper);
        List<Long> postList = sysUserPosts.stream().map(SysUserPost::getPostId).collect(Collectors.toList());
        return postList;
    }
}

